package com.asset.dispoal.business.repayment.service.factory;

import com.asset.dispoal.business.bsCase.domain.db.entity.BsCase;
import com.asset.dispoal.business.repayment.dto.RepaymentInfoResponse;
import com.asset.dispoal.business.repayment.service.strategy.IRepaymentStrategy;
import com.asset.dispoal.commom.menus.AgreementCaseSateMenu;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 生产还款信息
 * @author: yf
 * @date: 2022-09-26 16:50:16
 */
@Service("repaymentFactory")
public class RepaymentFactoryImpl implements IRepaymentFactory, ApplicationContextAware {
    final private static Map<String, String> CODE_MAPPING_MAP = new HashMap<>(AgreementCaseSateMenu.values().length);

    @PostConstruct
    void init() {
        //------未制作状态
        CODE_MAPPING_MAP.put("gather", "notMadeState");
        CODE_MAPPING_MAP.put("waitDeliver", "notMadeState");
        CODE_MAPPING_MAP.put("downloadOrder", "notMadeState");
        CODE_MAPPING_MAP.put("waitRecvOrder", "notMadeState");
        CODE_MAPPING_MAP.put("make", "notMadeState");
        //----- 未立案的状态
        CODE_MAPPING_MAP.put("making", "waitUpCaseState");
        CODE_MAPPING_MAP.put("waitUpCase", "waitUpCaseState");
        CODE_MAPPING_MAP.put("preLitigationMediation", "waitUpCaseState");
        //----- 立案状态
        CODE_MAPPING_MAP.put("registerCase", "registerCaseState");
        //-----  判决
        CODE_MAPPING_MAP.put("judgment", "judgmentState");
        //----- 调解状态
        CODE_MAPPING_MAP.put("mediation", "mediationState");
        //----- 和解状态
        CODE_MAPPING_MAP.put("reconciliation", "reconciliationState");


    }

    private static ApplicationContext applicationContext;

    public IRepaymentStrategy getService(String caseState) {
        return applicationContext.getBean(caseState, IRepaymentStrategy.class);
    }

    @Override
    public RepaymentInfoResponse createRepaymentInfo(BsCase bsCase) {
        String defaultValue = bsCase.getCaseState() + "State";
        IRepaymentStrategy repaymentStrategy = this.getService(CODE_MAPPING_MAP.getOrDefault(bsCase.getCaseState(), defaultValue));
        return repaymentStrategy.getRepaymentInfo(bsCase);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
